import Vue from 'vue';
import VueRouter from 'vue-router';
import MyLogin from '@/components/MyLogin'
import MyHome from '@/components/MyHome'

import MyUsers from '@/components/menus/MyUsers'
import MyGoods from '@/components/menus/MyGoods'
import MyOrders from '@/components/menus/MyOrders'
import MySettings from '@/components/menus/MySettings'
import MyRights from '@/components/menus/MyRights'

import MyUserDetail from '@/components/user/MyUserDetail.vue'


import pathArr from '@/router/pathArr.js'

Vue.use(VueRouter);

const router = new VueRouter({
    routes: [{
        path: '/',
        redirect: '/login'
    }, {
        path: '/login',
        component: MyLogin
    }, {
        path: '/home',
        component: MyHome,
        children: [{
                path: '/home/users',
                component: MyUsers,
            },
            {
                path: '/home/goods',
                component: MyGoods
            },
            {
                path: '/home/orders',
                component: MyOrders
            },
            {
                path: '/home/rights',
                component: MyRights
            }, {
                path: '/home/settings',
                component: MySettings
            }, {
                path: '/home/users/MyUserDetail/:id',
                component: MyUserDetail,
                props: true
            },
        ]
    }]
});

// 全局前置守卫
router.beforeEach(function(to, from, next) {
    if (pathArr.indexOf(to.path) !== -1) {
        const token = localStorage.getItem('token')
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})


export default router;